<template>
  <el-card class="box-card">
    <template #header>
      <div class="card-header">
        <span>留言列表</span>
      </div>
    </template>
    <el-row class="row-bg" justify="space-between">
      <el-col :span="4">
        <el-input
          v-model="pageData.keywords"
          placeholder="关键词"
          class="height40"
          style="width: 400px"
          clearable
          @clear="getUserMsgList"
        >
          <template #append>
            <div class="curr" @click="getUserMsgList">搜索</div>
          </template>
        </el-input>
      </el-col>
    </el-row>
    <el-table
      :data="msgListArr"
      style="width: 100%"
      class="margintop"
      v-loading="isLoading"
      :header-cell-style="{ 'border-bottom': 'none' }"
      :header-row-style="{ height: '60px' }"
      :cell-style="{ height: '60px', 'border-bottom': 'none' }"
      stripe
    >
      <el-table-column prop="nickName" label="昵称" />
      <el-table-column prop="mobile" label="手机" />
      <el-table-column prop="content" label="内容" />
      <el-table-column prop="addtime" label="留言时间" />
      <el-table-column fixed="right" label="操作" width="220" align="center">
        <template #default="scope">
          <el-popconfirm
            confirm-button-text="删除"
            cancel-button-text="取消"
            icon-color="#626AEF"
            title="确认删除"
            @confirm="onClickDelete(scope.row)"
          >
            <template #reference>
              <el-icon size="20">
                <Delete />
              </el-icon>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <div class="right" style="margin-top: 20px">
      <el-config-provider :locale="zhCn">
        <el-pagination
          v-model:current-page="pageData.page"
          v-model:page-size="pageData.pageSize"
          :page-sizes="[10, 20, 30]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageData.total"
          @size-change="getUserMsgList"
          @current-change="getUserMsgList"
        />
      </el-config-provider>
    </div>
  </el-card>
</template>
<script setup>
import { ref, reactive } from 'vue';
import { userMsgList, userMsgDel } from '@/api/manage/manage.js';
import zhCn from 'element-plus/es/locale/lang/zh-cn';
import { ElMessage } from 'element-plus';

const pageData = reactive({
  page: 1,
  pageSize: 10,
  total: 0,
  keywords: '',
});

const isLoading = ref(false); // 是否正在加载
const msgListArr = ref([]); // 留言列表数组

getUserMsgList();
// 获取留言列表
function getUserMsgList() {
  Object.keys(pageData).forEach(function (key) {
    if (pageData[key] === '') {
      delete pageData[key];
    }
  });

  isLoading.value = true;
  userMsgList(pageData)
    .then((res) => {
      isLoading.value = false;
      msgListArr.value = res.data.data;
      pageData.total = res.data.total;
    })
    .catch(() => {
      isLoading.value = false;
    });
}

/**
 * 删除留言
 * @param {Object} item 留言数据
 */
function onClickDelete(item) {
  isLoading.value = true;
  userMsgDel({ id: item.id })
    .then(() => {
      ElMessage({
        type: 'success',
        message: '删除成功',
      });

      getUserMsgList();
    })
    .catch(() => {
      isLoading.value = false;
    });
}
</script>
<style lang="scss" scoped>
::v-deep(.el-form--inline .el-form-item) {
  margin-right: 7px;
}

::v-deep(.el-form-item--default) {
  margin-bottom: 4px;
}

.box-card {
  margin: 20px;

  .card-header {
    font-size: 20px;
    font-weight: 600;
    color: #1d2129;
    padding: 10px;
  }
}
</style>
